const SessionServices = require('../services/sessionServices')

class SessionControllers {
  constructor() {
    this.getSessionByUserId = this.getSessionByUserId.bind(this)
  }
  
  // 统一响应函数
  handleRequest = async (serviceMethod, req, res, successStatus = 200) => {
    try {
      const result = await serviceMethod(req)
      if (result.code === 0) {
        res.status(successStatus).json(result) // 成功
      } else {
        res.status(result.statusCode || 400).json(result) // 失败
      }
    } catch (error) {
      console.log(error)
      res.status(500).json({
        code: 1,
        msg: '服务器内部错误',
      })
    }
  }

  async getSessionByUserId(req, res)
  {
    await this.handleRequest(
      () => SessionServices.getSessionByUserId(req.params.userid),
      req,
      res
    )
  }
}

module.exports = new SessionControllers